package com.test.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbUtil {
    private String dbUrl = "jdbc:mysql://localhost:3306/db_classmate?useSSL=true";
    private String dbUserName = "root";
    private String dbPassword = "WDNMD777";
    private String jdbcName = "com.mysql.jdbc.Driver";

    /**
     * 获得数据库连接
     * @return Connection
     * @throws Exception
     */
    public Connection getConnection() throws Exception{
        Class.forName(jdbcName);
        return DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
    }
    /**
     * 关闭数据库连接
     * @param con
     * @throws Exception
     */
    public void closeCon(Connection con)throws Exception{
        if(con!=null) {
            con.close();
        }
    }
    public static void main(String[] args) {
        DbUtil dbUtil = new DbUtil();
        try {
            dbUtil.getConnection();
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }
}